package views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.TimerTask;
import java.util.prefs.BackingStoreException;

import javax.print.attribute.standard.DateTimeAtCompleted;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;

import utils.Options;
import utils.TaskFactory;

import master.ChildInterface;
import net.miginfocom.swing.MigLayout;
import network.MasterCommunicator;


public class MainWindow
{

	JFrame mainFrame;
	JTextArea chatArea;
	JTextArea playersArea;
	JButton readyButton;
	JButton exitButton;
	JTextField chatInput;
	JPanel mainPanel;
	JScrollPane chatPanel;
	int lastMessageId = 0;
	String username;
	MasterCommunicator comm;
	public MainWindow()
	{
		tryToLogIn();

		mainFrame = new JFrame("Draw With Me");
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setSize(new Dimension(800, 600));

		mainPanel = new JPanel(new MigLayout());

		addWidgets();
		addListeners();

		mainFrame.getContentPane().add(mainPanel);
		mainFrame.pack();
		mainFrame.setVisible(true);

		username = Options.getInstance().getUser_name();
		this.comm = MasterCommunicator.getMasterCommunicator();
		comm.setMainWindowRef(this);
	}

	private void authorizeUser()
	{
	}

	public int getLastChatId()
	{
		return lastMessageId;
	}

	private void addWidgets()
	{
		chatArea = new JTextArea("");
		playersArea = new JTextArea("");
		readyButton = new JButton("Ready");
		exitButton = new JButton("Exit");
		chatInput = new JTextField("");
		chatPanel = new JScrollPane(chatArea);

		chatArea.setEditable(false);
		playersArea.setEditable(false);

		JPanel buttonsPanel = new JPanel(new MigLayout());
		buttonsPanel.add(readyButton);
		buttonsPanel.add(exitButton);

		mainPanel.add(new JLabel("Chat:"),"wrap");
		mainPanel.add(chatPanel, "width 600, height 400, wrap");
		mainPanel.add(chatInput, "width 600, wrap");
		mainPanel.add(buttonsPanel);
		
	}
	private void tryToLogIn()
	{
		//PasswordDialog pd = new PasswordDialog();
		//pd.show();
		//addMasterTask(MasterTask.CreateLogInTask(pd.getName(), pd.getPass(), null));
	}
	public void startGame()
	{
		new GameWindow(mainFrame);
	}
	private void addListeners()
	{
		this.readyButton.addMouseListener(new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				comm.addTask(TaskFactory.createOnStartButtonTask());
			}

			@Override
			public void mouseEntered(MouseEvent arg0)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent arg0)
			{
				// TODO Auto-generated method stub

			}

		});

		this.exitButton.addMouseListener(new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent e)
			{
				System.exit(0);

			}

			@Override
			public void mouseEntered(MouseEvent e)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e)
			{
				// TODO Auto-generated method stub

			}

		});

		this.chatInput.addKeyListener(new KeyListener()
		{

			@Override
			public void keyPressed(KeyEvent arg0)
			{
				if (arg0.getKeyCode() == 10)
				{
					String msg = username + ": " + chatInput.getText();
					comm.addTask(TaskFactory.createAddChatMessageTask(msg));
					chatInput.setText("");
				}
			}

			@Override
			public void keyReleased(KeyEvent e)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void keyTyped(KeyEvent e)
			{
				// TODO Auto-generated method stub

			}

		});

	}

	public static void createAndShowGUI()
	{
		
		MainWindow mw = new MainWindow();
	}

	private void addPlayer(String name)
	{
		String actualText = this.playersArea.getText();
		actualText += name;
		actualText += "\n";
		playersArea.setText(actualText);
	}

	public void addChatMessage(String text)
	{
		String actualText = this.chatArea.getText();
		actualText += text + "\n";
		chatArea.setText(actualText);
	}

	public void addChatMessages(ArrayList<String> messages)
	{
		lastMessageId += messages.size();
		for (String msg : messages)
			addChatMessage(msg);
	}

	private void closeWindow()
	{
		mainFrame.dispose();
	}

	private void switchToGame()
	{

	}

	
}
